package team526.service.impl;

//import javafx.scene.media.MediaPlayer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import team526.dao.TagDao;
import team526.dao.VideoDao;
import team526.entity.Video;
import team526.service.VideoService;
import team526.util.FileSave;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * #Date 2021/9/16 - 21:04
 *
 * @author 王铭 20181620310021
 */
@Service
@Transactional
public class VideoServiceImpl implements VideoService {
    @Autowired
    VideoDao videoDao;

    @Autowired
    FileSave fileSave;

    @Autowired
    TagDao tagDao;


    @Override
    public Video saveVideoWithFullPart(Integer uuid, String title, Integer main_tag_id, String desc, Integer length, List<Integer> sub_tag_ids, List<String> new_tag_titles, MultipartFile video_file, MultipartFile video_pic) throws IOException {
        String storage = Long.toHexString(System.currentTimeMillis());
        String video_storage = "v" + storage + ".mp4";
        String pic_storage = "p" + storage + ".jpg";
        if (video_file.isEmpty() || video_pic.isEmpty())
            return null;
        if (videoDao.createVideo(uuid, title, desc, length, new Date(), storage, storage) == 0) {
            return null;
        }
        Video video = videoDao.getVideoByStorage(storage);
        tagDao.addTagForVideo(video.getVideoid(), main_tag_id);

        if (sub_tag_ids != null)
            for (Integer id : sub_tag_ids) {
                tagDao.addTagForVideo(video.getVideoid(), id);
            }
        if (new_tag_titles != null)
            for (String s : new_tag_titles) {
                tagDao.addTag(s, main_tag_id);
                tagDao.addTagForVideo(video.getVideoid(), tagDao.getTagByName(s).getTag_id());
            }

        fileSave.saveApi2(video_storage, video_file.getBytes());
        fileSave.saveApi2(pic_storage, video_pic.getBytes());
        return video;
    }


}
